/**
 +-----------------------------------------------------------------------------------------
 * jQuery 			Plugins
 * @url				http://jqueryui.com
 * @Includes		QLoading,Qval
 * @Copyright 		2014 jQuery Foundation and other contributors; Licensed MIT 
 * @Author			liuricheng
 * @Time			2014-03-13
 +-----------------------------------------------------------------------------------------
 */

/**
 * Qdialog 
 * author	liuricheng
 * time		201-09-26
 * 
 */ 
(function($){  
	$.fn.Qdialog	= function(options){
		/* dialog对象 */
		var mDialog		=	$(this);
		/* 初始化参数 */
		var defaults 	=	{
			autoOpen	: true,
			modal		: true,
			showTitle   : false,
			title		: '',
			close		: function(){
				mDialog.dialog('close');			
			}
		}
		/* 合并参数 */
		var options		=	$.extend({},defaults,options);	
		/* 处理参数 */
		if(options.title == ''){
			options.title = '标题';
		}	
		
		/* 显示关闭按钮 */	
		mDialog.prepend('<span id="qs-ui-close-buttton" class="ui-button-text qs-close-button-1">X</span>');					
		/* 显示title */
		if(true == options.showTitle){
			mDialog.prepend('<div id="qs-ui-title-bar" class="qs-ui-title-bar"><h1 class="qs-ui-title">' + options.title + '</h1></div>');
		}
		/* 显示dialog */
		mDialog.dialog(options);	
		
		$('#qs-ui-close-buttton').bind('click',function(){
			/* 移除添加的按钮和标题 */
			$(this).remove();
			$('#qs-ui-title-bar').remove();
			mDialog.dialog('close');			
		});
		/* 返回JS对象 */ 
		return mDialog;
	};
})(jQuery);
 
 
/**
 * 拓展jQuery插件函数
 * @Abstract	需加载插件: jquery.js + jquery-ui.css + jquery-ui.js
 * @param		
 *      {
 *       	img:'right', // error,right,happy,sad,warm
 *			text:'加载中',
 *          time:0，
 *			status:'open'，
 *			model:true
 *		}
 */ 
(function($){  
    $.fn.extend({   
		QLoading : function(opt){
			/* Settings */
			var defaults = {
				'img'	: 'right',
				'text'	: '加载中',
				'status': 'open',
				'time'	: 0,
				'modal'	: true		// 显示遮罩层,false:不显示;true:显示
			}
			var _dialog		=	$(this);
			/* 判断参数 */
			if(arguments.length==1){			
				if(typeof opt == 'object'){				
					defaults	=	$.extend(defaults,opt);
				}
				else if(typeof opt == 'string'){
					defaults.status	=	opt;
				}
			}
			else if(arguments.length==2){
				var key		=	arguments[1];
				var value	=	arguments[2];
				defaults	=	$.extend(defaults,{key:value});
			}						
			/* Loading的内容 */
			var html 		=	'<span id="loading_img" class="loading_img" >加载中...</span>';			
			$(this).html(html);
			(defaults.img == "right") ? $('#loading_img').attr('class','loading_img') : $('#loading_img').attr('class',defaults.img + "_img");
			$('#loading_img').text(defaults.text);			
			
			if('open' == defaults.status){
				$(this).removeClass('hide'); // 兼容写法
          		$(this).dialog({
          	    	minHeight	: 50,
          	    	width		: 'auto',
          	    	modal		: defaults.modal 
          	    }).dialog("open");
				/* 需要打开多长时间 */
				if(defaults.time > 0){
					setTimeout(function(){ 
						_dialog.dialog('close');
					},defaults.time * 1000); 
				}
          	}
          	else if("close" == defaults.status){
				if(defaults.time > 0){
					setTimeout(function(){ 
						_dialog.dialog('close');
					},defaults.time * 1000); 
				}
				else
					$(this).dialog('close');
          	}
        }  
    });  
})(jQuery);

/**
 * @获取input的输入值 
 * @Abstract	需加载插件: jquery.js+
 * @type		'':所有表单元素,"input['type=radio']":所有符合条件的input元素,'select':所有的下拉框元素
 */
(function($){
	$.fn.QVal = function(type){
		if(type == undefined || type == 'undefined') 
			type = '';	
		else if (type == 'all')	
			type = '';
		var GTable 	= 	$(this); //获取对象
		var datas 	= 	{};
		switch(type){
			case '':		
				GTable.find('[name]').each(function(){
					//alert($(this).attr('name') + $(this).val());
					datas[$(this).attr('name')] = $(this).val();		
				});
			break;
			case 'input':
				var inputs = GTable.find('input');
				inputs.each(function(){
					//alert($(this).attr('name'));
					datas[$(this).attr('name')] = $(this).val();		
				});
			break;
			case 'select':
				var selects = GTable.find('select');
				selects.each(function(){
					//alert($(this).attr('name'));
					datas[$(this).attr('name')] = $(this).val();		
				});
			break;
			default:
			break;
		}
		return datas;							
	};	
})(jQuery);

/**
 * 跳转页面
 * @msg		{'num': '', 'msg':'','url': ''}
 *
 */ 
function navigate(msg){	
	window.location.href	=	msg.url;
} 

/*!
 * jQuery Cookie Plugin v1.3.1
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2013 Klaus Hartl
 * Released under the MIT license
 */
(function (factory) {
	if (typeof define === 'function' && define.amd) {
		// AMD. Register as anonymous module.
		define(['jquery'], factory);
	} else {
		// Browser globals.
		factory(jQuery);
	}
}(function ($) {

	var pluses = /\+/g;

	function raw(s) {
		return s;
	}

	function decoded(s) {
		return decodeURIComponent(s.replace(pluses, ' '));
	}

	function converted(s) {
		if (s.indexOf('"') === 0) {
			// This is a quoted cookie as according to RFC2068, unescape
			s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
		}
		try {
			return config.json ? JSON.parse(s) : s;
		} catch(er) {}
	}

	var config = $.cookie = function (key, value, options) {

		// write
		if (value !== undefined) {
			options = $.extend({}, config.defaults, options);

			if (typeof options.expires === 'number') {
				var days = options.expires, t = options.expires = new Date();
				t.setDate(t.getDate() + days);
			}

			value = config.json ? JSON.stringify(value) : String(value);

			return (document.cookie = [
				config.raw ? key : encodeURIComponent(key),
				'=',
				config.raw ? value : encodeURIComponent(value),
				options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
				options.path    ? '; path=' + options.path : '',
				options.domain  ? '; domain=' + options.domain : '',
				options.secure  ? '; secure' : ''
			].join(''));
		}

		// read
		var decode = config.raw ? raw : decoded;
		var cookies = document.cookie.split('; ');
		var result = key ? undefined : {};
		for (var i = 0, l = cookies.length; i < l; i++) {
			var parts = cookies[i].split('=');
			var name = decode(parts.shift());
			var cookie = decode(parts.join('='));

			if (key && key === name) {
				result = converted(cookie);
				break;
			}

			if (!key) {
				result[name] = converted(cookie);
			}
		}

		return result;
	};
	config.defaults = {};
	$.removeCookie = function (key, options) {
		if ($.cookie(key) !== undefined) {
			// Must not alter options, thus extending a fresh object...
			$.cookie(key, '', $.extend({}, options, { expires: -1 }));
			return true;
		}
		return false;
	};

}));

/**
 * tableUI 1.0
 * Copyright (c) 2014 liuricheng http://yinbaner.com/
 * Date:2014-09-24
 *
 */
(function($){
	$.fn.tableUI = function(options){	
		var defaults = {
			evenRowClass:'even',
			oddRowClass:'odd',
			activeRowClass:'active'
		}		
		var options = $.extend(defaults,options);
		this.each(function(){
			var thisTable 	=	$(this);
			/* 数据行标志属性:odd,even */
			$(thisTable).find('tr.data').each(function(n){				
				if( ((n +1) % 2) == 0) $(this).addClass('even');
				else $(this).addClass('odd');				
			});
			$(thisTable).find("tr.even").addClass('even');
			//(奇数行设置成#ccc颜色)
			$(thisTable).find("tr.even").addClass('even');
			$(thisTable).find("tr.odd").addClass('odd');
			//(把背景色保存到属性中)
			$(thisTable).find("tr.data").mouseover(function(){
				//$(this).css("background","#000");
				$(this).addClass('active');
			});	
			//(当鼠标移到表格上是，当前一行背景变色)
			$(thisTable).find("tr.data").mouseout(function(){
				$(this).removeClass('active');
			});			
		});
	};	
})(jQuery);


/**
 * 无刷帅选数据
 * Author	liuricheng
 * time		2014-09-26
 *
 */
/* 文章帅选 */
var Qfilter		=	new Object();

/* 提交地址 */
Qfilter.url		=	'';

/* init url */
Qfilter.init 	=	function(url){
	if(url == '' || typeof url == 'undefined') url = '';
	Qfilter.url = url;	
}
	
Qfilter.search	=	function(id){
	/* 判断参数的个数 */
	var targetId	=	(arguments.length == 1) ? 'content' : arguments[1];		
	/* 获取id内所有的提交元素的值 */
	var datas		=	$('#' + id).QVal();
	
	var options = {
		url	  	: Qfilter.url,
		type  	: 'post',
		dataType: 'json',
		data   	: datas,
		beforeSend:function(){
			$('#loading').QLoading({text:'加载中...',status:'open'});
		},
		success:function(result,status){
			$('#' + targetId).html('').html(result.info);
			var tableId		=	$('#' + targetId).find('table.qs-table').attr('id');
			$('#' + tableId).tableUI();	
			$('#loading').QLoading({text:'成功',img:'happy',status:'close',time:0.5});			
		}
	}
	$.ajax(options);
}


/*
 *
 * Copyright (c) 2014 Daniele Lenares (https://github.com/Ryuk87)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 0.5.1
 * Function:
	$.goup({
		trigger: 100,
		bottomOffset: 150,
		locationOffset: 100,
		title: 'This is a Demo',
		titleAsText: true
	});
 
 */
(function ( $ ) {	
	$.goup = function(user_params) {		
		/* Default Params */
		var params = $.extend({
				location : 'right',
				locationOffset : 20,
				bottomOffset : 10,
				containerRadius : 10,
				containerClass : 'goup-container',
				arrowClass : 'goup-arrow',
				alwaysVisible : false,
				trigger: 500,
				entryAnimation : 'fade',
				goupSpeed : 'slow',
				hideUnderWidth : 500,
				containerColor : '#000',
				arrowColor : '#fff',
                title : '',
                titleAsText : false,
                titleAsTextClass : 'goup-text'
			}, user_params);
		/* */
		
		
		$('body').append('<div style="display:none" class="'+params.containerClass+'"></div>');
		var container = $('.'+params.containerClass);
		$(container).html('<div class="'+params.arrowClass+'"></div>');
		var arrow = $('.'+params.arrowClass);
		
		/* Parameters check */
		var location = params.location;
		if (location != 'right' && location != 'left') {
			location = 'right';
		}
		
		var locationOffset = params.locationOffset;
		if (locationOffset < 0) {
			locationOffset = 0;
		}
		
		var bottomOffset = params.bottomOffset;
		if (bottomOffset < 0) {
			bottomOffset = 0;
		}
		
		var containerRadius = params.containerRadius
		if (containerRadius < 0) {
			containerRadius = 0;
		}
		
		var trigger = params.trigger;
		if (trigger < 0) {
			trigger = 0;
		}
		
		var hideUnderWidth = params.hideUnderWidth;
		if (hideUnderWidth < 0) {
			hideUnderWidth = 0;
		}
		
		var checkColor = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i;
		if (checkColor.test(params.containerColor)) {
			var containerColor = params.containerColor;
		} else {
			var containerColor = '#000';
		}
		if (checkColor.test(params.arrowColor)) {
			var arrowColor = params.arrowColor;
		} else {
			var arrowColor = '#fff';
		}
        
        if (params.title === '') {
            params.titleAsText = false;
        }
		/* */
		
		/* Container Style */
		var containerStyle = {};
		containerStyle = {
			position : 'fixed',
			width : 40,
			height : 40,
			background : containerColor,
			cursor: 'pointer'
		};
		containerStyle['bottom'] = bottomOffset;
		containerStyle[location] = locationOffset;
		containerStyle['border-radius'] = containerRadius;
		
		$(container).css(containerStyle);
        if (!params.titleAsText) {
            $(container).attr('title', params.title);
        } else {
            $('body').append('<div class="'+params.titleAsTextClass+'">'+params.title+'</div>');
            var textContainer = $('.'+params.titleAsTextClass);
            $(textContainer).attr('style', $(container).attr('style'));
            $(textContainer).css('background','transparent')
                           .css('width',80)
                           .css('height','auto')
                           .css('text-align','center')
                           .css(location,locationOffset - 20);
            var containerNewBottom = $(textContainer).height() + 10;
            $(container).css('bottom', '+='+containerNewBottom+'px');
        }
            
		
		/* Arrow Style */		
		var arrowStyle = {};
		arrowStyle = {
			width : 0,
			height : 0,
			margin : '0 auto',
			'padding-top' : 13,
			'border-style' : 'solid',
			'border-width' : '0 10px 10px 10px',
			'border-color' : 'transparent transparent '+arrowColor+' transparent' 
		};
		$(arrow).css(arrowStyle);
		/* */
		
		
		
		/* Trigger Hide under a certain width */
		var isHidden = false;
		$(window).resize(function(){
			if ($(window).outerWidth() <= hideUnderWidth) {
				isHidden = true;
				do_animation($(container), 'hide', params.entryAnimation);
                if (textContainer)
                    do_animation($(textContainer), 'hide', params.entryAnimation);
			} else {
				isHidden = false;
				$(window).trigger('scroll');
			}
		});
		/* If i load the page under a certain width, i don't have the event 'resize' */
		if ($(window).outerWidth() <= hideUnderWidth) {
			isHidden = true;
			$(container).hide();
            if (textContainer)
                $(textContainer).hide();
		}
		
		
		/* Trigger show event */
		if (!params.alwaysVisible) {
			$(window).scroll(function(){
				if ($(window).scrollTop() >= trigger && !isHidden) {
					do_animation($(container), 'show', params.entryAnimation);
                    if (textContainer)
                        do_animation($(textContainer), 'show', params.entryAnimation);
				}
				
				if ($(window).scrollTop() < trigger && !isHidden) {
					do_animation($(container), 'hide', params.entryAnimation);
                    if (textContainer)
                        do_animation($(textContainer), 'hide', params.entryAnimation);
				}
			});
		} else {
			do_animation($(container), 'show', params.entryAnimation);
            if (textContainer)
                do_animation($(textContainer), 'show', params.entryAnimation);
		}
		/* If i load the page and the scroll is over the trigger, i don't have immediately the event 'scroll' */
		if ($(window).scrollTop() >= trigger && !isHidden) {
			do_animation($(container), 'show', params.entryAnimation);
            if (textContainer)
                do_animation($(textContainer), 'show', params.entryAnimation);
		}
		
		/* Click event */
		$(container).on('click', function(){
			$('html,body').animate({ scrollTop: 0 }, params.goupSpeed);
			return false;
		});		
        
        $(textContainer).on('click', function(){
			$('html,body').animate({ scrollTop: 0 }, params.goupSpeed);
			return false;
		});
	};
	
	
	/* Private function for the animation */
	function do_animation(obj, type, animation) {
		if (type == 'show') {
			switch(animation) {
				case 'fade':
					obj.fadeIn();
				break;
				
				case 'slide':
					obj.slideDown();
				break;
				
				default:
					obj.fadeIn();
			}
		} else {
			switch(animation) {
				case 'fade':
					obj.fadeOut();
				break;
				
				case 'slide':
					obj.slideUp();
				break;
				
				default:
					obj.fadeOut();
			}
		}
	}
	
}( jQuery ));

/**
 * 抖动插件
 * 
 *
 */
(function($){ 
	/**
	 * jquery 抖动插件
	 * 用法:
		 var defaults = {
			Event : "click", //触发响应事件
			direction : 0,   //默认为0、左右方向，1、上下方向
			isAuto : false,  //是否自动抖动           
			time : 3000,     //设置自动抖动时间，前提是isAuto=true
			speed : 10,      //抖动的速度
			isCycle : false  //是否循环 
		};
		$("#autoPanel").Shake({direction:1,isAuto:true,isCycle:true});
		$("#clickPanel").Shake();
	 */
	$.fn.Shake = function(options){
		var defaults = {
			Event : "click",								//触发响应事件
			direction : 0,									//默认为0、左右方向，1、上下方向
			isAuto : false,									//是否自动抖动			
			time : 2000,									//设置自动抖动时间，前提是isAuto=true
			speed : 50,										//抖动的速度
			isCycle : false  								//是否循环	
		};
		var options = $.extend(defaults,options);		
		var $this = $(this);								//当然响应事件对象		
		//文本框绑定事件
		$this.die().live(options.Event,function(e){											  
			$(this).css({'position':'absolute'}); 
			var offset = $(this).offset();
			if (options.direction==0){
				for(var i=1; 4>=i; i++){   
					$this.animate({left:offset.left-(40-10*i)},options.speed);   
					$this.animate({left:offset.left+2*(40-10*i)},options.speed);   
				} 
			}else{
				for(var i=1; 4>=i; i++){   
					$this.animate({top:offset.top-(40-10*i)},options.speed);   
					$this.animate({top:offset.top+2*(40-10*i)},options.speed);   
				} 
			}	
		});	
		if (options.isAuto){
			var st = setInterval(function (){			
				$this.trigger(options.Event);
				if(!options.isCycle){
					clearTimeout(st);
				}
			},options.time);	
		}
	};		
	/**
	 * jQuery 抖动插件
	 * @param	intShakes 	Amount of shakes
	 * @param	intDistance Shake distance
	 * @param	intDuration Time duration
	 *
	 * example:
		$(function() {
			$('#btn').click(function() {
				$(this).Shake(2,10,400);
			});
		});
	 */
	$.fn.Qshake = function(intShakes /*Amount of shakes*/, intDistance /*Shake distance*/, intDuration /*Time duration*/) {
		this.each(function() {
			var jqNode = $(this);
			jqNode.css({position: 'relative'});
			for (var x=1; x<=intShakes; x++) {
				jqNode.animate({ left: (intDistance * -1) },(((intDuration / intShakes) / 4)))
				.animate({ left: intDistance },((intDuration/intShakes)/2))
				.animate({ left: 0 },(((intDuration/intShakes)/4)));
			}
		});
		return this;
	}
	
	
	
}( jQuery ));				
				

	


